/* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for 
 * full list of contributors). Published under the Clear BSD license.  
 * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
 * full text of the license. */

/**
 * Class: OpenLayers.LocalTileStorage.TileSQLStorage
 * Class for access to SQL Web Storage or Web (local) Storage.
 * Allows you to store, retrieve and get information about what
 * is stored. 
 * 
 * Inherits from:
 *  - <OpenLayers.LocalTileStorage>
 */

/**
 * @requires OpenLayers/Layer.js
 */

OpenLayers.LocalTileStorage.TileSQLStorage =  OpenLayers.Class(OpenLayers.LocalTileStorage, {
	
    name:null,
    version:null,
    description:null,
    size:null,
    layer: null,
	db: null,
	
	tileTableName: "NGTiles",
		  
	initialize: function(name, version, description, size, layer, tileTableName) {
	    try {
		  this.name = name;
		  this.version = version;
		  this.description = description;
		  this.size = size;
		  this.layer = layer,
		  this.db = openDatabase(name, version, description, size);
		  
		  if (tileTableName)
			  this.tileTableName = tileTableName;
		  
		  tileTableName = this.tileTableName; 
		  
		  this.db.transaction( 
			function(tx) {
				tx.executeSql("CREATE TABLE IF NOT EXISTS " + tileTableName  +  " (key TEXT, value BLOB) ",
				[], 
				function(tx, result) { 
		       		console.log("Table: " + tileTableName + " created.");
				}, function(tx, error) {
					/* table may already exist */
			       	console.debug(error);
				});	
		  });
		  
	    } catch (e) {
	    	throw new Error("Web SQL storage is not supported.");
	    }
	    return this;

	},
	set: function(key, value) {
		// TODO Check for existing entries to prevent duplicates.
		var tileTableName = this.tileTableName; 

		this.db.transaction( 
			function(tx) {
				tx.executeSql("Select 1 from " + tileTableName + " where key=?",
				[key], 
				function(tx, result) {
					if(result.rows.length <= 0){
						tx.executeSql("Insert into " + tileTableName + " (key, value) values (?,?)",
							[key,value], 
							function(tx, result) { 
			            		/* No results as it's an insert */
							}, function(tx, error) {
				    	    	console.debug(error);
						});
					}
			    }, 
			    function(tx, error) {
		        	console.debug(error);
				});
		});		
	},
	get: function(key, imgDiv) {
		var tileTableName = this.tileTableName; 

		this.db.transaction( 
			function(tx) {
				tx.executeSql("Select value from " + tileTableName + " where key=?",
				[key], 
				function(tx, result) {
				  if(result.rows.length){
					console.log("VT'de bulundu...");
					imgDiv.src = result.rows.item(0).value;
					return imgDiv.src;
				  }
			    }, function(tx, error) {
		        	console.debug(error);
				});
		});
	},
	
	clear: function() {
		var tileTableName = this.tileTableName; 

		this.db.transaction( 
				function(tx) {
					tx.executeSql("Delete from " + tileTableName,
					[], 
					function(tx, result) {
						console.log("All records deleted from " + tileTableName);
				    }, function(tx, error) {
			        	console.debug(error);
					});
			});
	},
	count: function(resultHandler) {
		var tileTableName = this.tileTableName;
		var count;
		var name = this.name;
		this.db.transaction(
				function(tx) {
					tx.executeSql("Select count(*) as count from " + tileTableName,
					[], 
					function(tx, result) {
					  if(result.rows.length){						
						 resultHandler(name,result.rows.item(0)['count']);
					  }
				    }, function(tx, error) {
			        	console.debug(error);
					});
				});
	},
  
  CLASS_NAME: "OpenLayers.LocalTileStorage.TileSQLStorage"
  });